import Pluto,struct,os,fnmatch,subprocess,codecs
fl=Pluto.walk('do')
for fn in fl:
        if fnmatch.fnmatch(fn,'*.txt'):
                continue
        print fn
        fp=open(fn,'rb')
        indexsize=struct.unpack('i',fp.read(4))[0]
        
        if not os.path.isdir(fn.split('.')[0]):
                os.mkdir(fn.split('.')[0])
        for i in xrange(0,indexsize,8):
                fp.seek(i)
                offset=struct.unpack('i',fp.read(4))[0]
                size=struct.unpack('i',fp.read(4))[0]
                fp.seek(offset)
                data=fp.read(size)
                tar=open(fn.split('.')[0]+'\\%04d.txt'%(i/8),'wb')
                tar.write(data)
                tar.close()
        fp.close()
        
fl2=Pluto.walk('do')
for fn2 in fl2:
        if fnmatch.fnmatch(fn2,'*.bin'):
                continue
        print fn2
        os.system('agblz u %s %s'%(fn2,fn2))
        fp2=codecs.open(fn2,'rb','utf8')
        text=fp2.read()
        fp2.close()
        tar2=codecs.open(fn2,'wb','utf16')
        tar2.write(text)
        tar2.close()
